﻿using DevComponents.DotNetBar.Metro;
using PlatForm.Controller;
using PlatForm.DataLayer;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PlatForm.Presentation
{
    public partial class frmArticle : MetroForm
    {
        ArticleController articleCtrl = new ArticleController();

        public frmArticle()
        {
            InitializeComponent();
            dGVArticle.AutoGenerateColumns = false;
        }

        private void Init()
        {
            var lstArticle = articleCtrl.GetList();
            dGVArticle.DataSource = lstArticle;

            var art_No = new Binding("Text", lstArticle, "ART_No");
            txtART_No.DataBindings.Clear();
            txtART_No.DataBindings.Add(art_No);

            var art_Name = new Binding("Text", lstArticle, "ART_Name");
            txtART_Name.DataBindings.Clear();
            txtART_Name.DataBindings.Add(art_Name);

            var art_Unit = new Binding("Text", lstArticle, "Unit");
            txtUnit.DataBindings.Clear();
            txtUnit.DataBindings.Add(art_Unit);

            var art_Cat = new Binding("Text", lstArticle, "Cat");
            cmbCat.DataBindings.Clear();
            cmbCat.DataBindings.Add(art_Cat);
            
            txtART_No.Focus();
        }

        private void frmArticle_Load(object sender, EventArgs e)
        {
            Init();
        }

        private void txtART_No_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar)
                && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            Delete();
        }

        public void Delete()
        {
            try
            {
                var msg = MessageBox.Show("Do you want to delete this record?", "Platform", MessageBoxButtons.YesNo);
                if (msg == DialogResult.Yes)
                {
                    articleCtrl.Delete(txtART_No.Text);
                    Init();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Can not delete this record");
            }
        }

        public void Save()
        {
            var article = new Article()
            {
                ART_No = txtART_No.Text,
                ART_Name = txtART_Name.Text,
                Cat = int.Parse(cmbCat.Text),
                Unit = float.Parse(txtUnit.Text),
                LastUpdated = DateTime.Now
            };
            try
            {
                var x = articleCtrl.Save(article);
                Init();
                if (x == 1)
                {
                    Clear();
                    dGVArticle.Rows[dGVArticle.Rows.Count - 1].Selected = true;
                    dGVArticle.FirstDisplayedScrollingRowIndex = dGVArticle.Rows.Count - 1;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Can not save record");
            }
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch (keyData)
            {
                case Keys.N | Keys.Control:
                    Clear();
                    break;
                case Keys.D | Keys.Control:
                    Delete();
                    break;
                case Keys.Enter:
                    Save();
                    break;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void Clear()
        {
            txtART_No.Text = "";
            txtART_Name.Text = "";
            txtUnit.Text = "";
            txtART_No.Focus();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            Clear();
        }
    }
}
